//
//  TTToTextView.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/13.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

class TTToTextView: UITextView {

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var label:UILabel={
    let label = UILabel()
        label.text = "请输入文字"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        
        return label
    }()
    
    override var font: UIFont?
        {
        
           didSet
           {
            if font != nil{
             label.font = font
              }
           }
       
       }
    
    var poshoder:String?
    {
        didSet{
            label.text = poshoder
        }
    
    }
    
    private func setupUI()
    {
//    backgroundColor = UIColor.red
        //注册通知,文字改变
        NotificationCenter.default.addObserver(self, selector: #selector(textChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
        addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
         addConstraint(NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 7))
        addConstraint(NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: -10))
    }
    
    //文字改变通知
    @objc private func textChange()
    {
       label.isHidden = hasText
        
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
